//
//  Labs_OC_Runtime_PrintDemoVC.m
//  APSwift
//
//  Created by jianbo.pei on 2023/8/22.
//
/**
 ## 参考链接：
 - [iOS 开发：『Runtime』详解（四）获取类详细属性、方法](https://juejin.cn/post/6844903903939526669)
 */

#import "Labs_OC_Runtime_PrintDemoVC.h"
#import <objc/runtime.h>

@interface Labs_OC_Runtime_PrintDemoVC ()

@end

@implementation Labs_OC_Runtime_PrintDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    Class cls = [UINavigationController class];
    [self printIvarListFor:cls];
    [self printPropertyList:cls];
    [self printMethodList:cls];
}

#pragma mark 打印成员变量列表
- (void)printIvarListFor: (Class)cls {
    unsigned int count;
    
    Ivar *ivarList = class_copyIvarList([cls class], &count);
    for (unsigned int i = 0; i < count; i++) {
        Ivar myIvar = ivarList[i];
        const char *ivarName = ivar_getName(myIvar);
        NSLog(@"ivar(%d) : %@", i, [NSString stringWithUTF8String:ivarName]);
    }
    
    free(ivarList);
}


#pragma mark 打印属性列表
- (void)printPropertyList: (Class)cls {
    unsigned int count;
    
    objc_property_t *propertyList = class_copyPropertyList(cls, &count);
    for (unsigned int i = 0; i < count; i++) {
        const char *propertyName = property_getName(propertyList[i]);
        NSLog(@"propertyName(%d) : %@", i, [NSString stringWithUTF8String:propertyName]);
    }
    
    free(propertyList);
}

#pragma mark 打印方法列表
- (void)printMethodList: (Class)cls {
    unsigned int count;
    
    Method *methodList = class_copyMethodList(cls, &count);
    for (unsigned int i = 0; i < count; i++) {
        Method method = methodList[i];
        NSLog(@"method(%d) : %@", i, NSStringFromSelector(method_getName(method)));
    }
    
    free(methodList);
}



@end
